package co.baiku.boot.message;

import java.util.HashMap;
import java.util.Map;

public enum WeixinError {
    SERVER_BUSY(-1, "系统繁忙，请稍候再试"),
    SUCCESS(0, "请求成功"),
    CODE_IS_INVALID(40029, "code 无效"),
    
    REQUEST_LIMIT(45011, "频率限制，每个用户每分钟100次"),

    MENU_NO_EXIST(46003, "菜单信息不存在"),

    UNKNOWN(99999, "未知"),
    ;

    private Integer errcode;
    private String errmsg;

    private static final Map<Integer, WeixinError> index = new HashMap<>();

    /**
     * 通过错误代码找错误枚举
     *
     * @param errcode
     * @return
     */
    public static WeixinError parse(Integer errcode) {
        if (index.isEmpty()) {
            for (WeixinError item : WeixinError.values()) {
                index.put(item.getErrcode(), item);
            }
        }
        return index.getOrDefault(errcode, UNKNOWN);
    }

    WeixinError(int errcode, String errmsg) {
        this.errcode = errcode;
        this.errmsg = errmsg;
    }

    public Integer getErrcode() {
        return errcode;
    }


    public String getErrmsg() {
        return errmsg;
    }

}
